from ConfigParser import SafeConfigParser as ConfigParser
import db

import os
CONFIG_FILE = os.path.join(os.environ['HOME'], '.wjs.ini')

from mako.template import Template

TEMPLATE = Template("""
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Games</title>
	<style type="text/css">
body {
	font-size:10pt;
	background:#dddddd;
	width:973px;
	margin:auto
}

.body {
	margin-top:11px;
	padding:10px;
	border:solid 1px #c3c3c3;
	background-color:rgb(96, 96, 82);
	color:white;
}

h2 {
	border-bottom:solid 1px #a32026;
	color:white;
	font-size:14pt;
}

.Bronze { color: rgb(255, 128, 0) }
.Silver { color: rgb(196, 196, 196) }
.Gold { color: rgb(255, 255, 0) }
.Platinum { color: rgb(128, 196, 255) }
.Hidden { color: rgb(148, 148, 148) }

table { width: 95% }
td { background-color:rgb(132, 132, 128) }
td.obtained { background-color:rgb(148, 148, 132) }
td.Hidden { background-color:rgb(96, 96, 82) }

td img { float:left }

.summary { font-size: 12pt; }

	</style>
</head>
<body>
<%	global g_count, g_obtained, g_bronze, g_silver, g_gold, g_platinum
	g_count, g_obtained, g_bronze, g_silver, g_gold, g_platinum = 0, 0, 0, 0, 0, 0 %>

<%def name="print_counter(counter)">
	% if counter.obtained == counter.count:
		<p>All ${counter.count} Trophies earned !</p>
	% else:
		<p>${counter.obtained} / ${counter.count} Trophies earned</p>
	% endif
<p>
	<span title="Bronze trophies" class="Bronze">${counter.bronze}</span> /
	<span title="Silver trophies" class="Silver">${counter.silver}</span> /
	<span title="Gold trophies" class="Gold">${counter.gold}</span> /
	<span title="Platinum trophies" class="Platinum">${counter.platinum}</span>
</p>
</%def>

<%def name="trophy_count(trophies)">
<%
	counter = TrophyCounter()
	for trophy in trophies:
		counter.add(trophy)
		total.add(trophy)
%>
<div class="summary">
	${print_counter(counter)}
</div>
</%def>
% for platform in db.Platform.select():
	<div class="body">
	<h1>${platform.name}</h1>
	% for game in platform.games:
		<h2>${game.name}</h2>
		% if game.releaseDate:
			<p>Release Date: ${game.releaseDate}</p>
		% endif
		% if game.icon:
			<img src="data:image/png;base64,${game.icon.encode('base64')}">
		% endif
		% if game.trophies.count() > 0:
			<h3>Trophies</h3>
			${trophy_count(game.trophies)}
			<table>
			<% column = 0 %>
			% for trophy in game.trophies:
				% if column == 0:
			<tr>
				% endif
				% if trophy.obtained:
					<td class="${trophy.type} obtained" title="${trophy.description}">
					<img src="data:image/png;base64,${trophy.icon.encode('base64')}">
					<div>${trophy.name}</div>
				% elif not trophy.hidden:
					<td class="${trophy.type}" title="${trophy.description}">
					<div>${trophy.name}</div>
				% else:
					<td class="${trophy.type}">
					(Hidden trophy)
				% endif
				</td>
				<% column += 1 %>
				% if column > 3:
					</tr>
					<% column = 0 %>
				% endif
			% endfor
			% if column != 0:
				</tr>
			% endif
			</table>
		% endif
	% endfor
	</div>
% endfor
<div class="body">
	${print_counter(total)}
</div>
</body>
</html>
""", input_encoding='utf-8', output_encoding='utf-8')

class TrophyCounter:
	def __init__(self):
		self.count = 0
		self.obtained = 0
		self.bronze = 0
		self.silver = 0
		self.gold = 0
		self.platinum = 0
	def add(self, trophy):
		self.count += 1
		if trophy.obtained:
			self.obtained += 1
			if trophy.type == 'Bronze':
				self.bronze += 1
			elif trophy.type == 'Silver':
				self.silver += 1
			elif trophy.type == 'Gold':
				self.gold += 1
			elif trophy.type == 'Platinum':
				self.platinum += 1

def main():
	config = ConfigParser()
	config.read(CONFIG_FILE)
	db.init(config.get("dsn", "games"))
	fp = open("report.html", 'w')
	fp.write(TEMPLATE.render(db = db, TrophyCounter = TrophyCounter, total = TrophyCounter()))
	fp.close()

if __name__ == '__main__':
	main()
